package org.smartblog.core.render;

import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import freemarker.template.Configuration;
import freemarker.template.Template;

/**
 * Create Date:2013-12-5
 * @author wanghx
 */
public class Render {
	
	private transient static final Configuration config = new Configuration();
	
	private Render(){
	}

	public static void init(ServletContext context,String path){
		
		config.setServletContextForTemplateLoading(context, path);
	}
	
	public static void rend(HttpServletRequest request,HttpServletResponse response,String view,Map<String,Object> root){
		PrintWriter writer = null;
        try {
			Template template = config.getTemplate(view);
			writer = response.getWriter();
			template.process(root, writer);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(writer != null)
				writer.close();
		}
		
	}
}
